//
//  CheckOutHistoryViewController.m
//  GYMWinners
//
//  Created by Logic Solutions on 4/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CheckOutHistoryViewController.h"
#import "BaseroutViewController.h"

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h"
#import "MBProgressHUD.h"
#import "BaseroutAPI.h"
#import "AppDelegate.h"

@interface CheckOutHistoryViewController ()

@end

@implementation CheckOutHistoryViewController
@synthesize checkinArray;
@synthesize historyTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.checkinArray = [[NSMutableArray alloc] init];
    
    NSMutableDictionary *slist = [[NSMutableDictionary alloc] init];
    [slist setObject:[[BaseroutAPI sharedInstance] zGetVarEx:@"customerid"] forKey:@"cid"];
    [self zSendServer:slist theTitle:@"请稍等" theCommand:@"/space/history"];
}

- (void)viewDidUnload
{
    [self setHistoryTableView:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - ASIFormDataRequest delegate

- (void)requestFinished:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (request.responseStatusCode == 200) {
        
        NSString *responseString = [request responseString];
        NSLog(@"responseString=[%@]", responseString);   
        NSDictionary *responseDict = [responseString JSONValue];
        NSArray *checkins = [responseDict valueForKey:@"checkins"];

        NSDictionary *checkin;
        for( int i=0; i < [checkins count]; i++ )
        {
            checkin = [checkins objectAtIndex:i];
            NSDictionary *location = [checkin objectForKey:@"location"];
            NSString *gymname = [location objectForKey:@"name"];
            NSNumber *startDate = [checkin objectForKey:@"startDate"];
            
            NSTimeInterval _internal = [startDate doubleValue] / 1000.0;
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:_internal];
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            NSString *sdate = [formatter stringFromDate:date];
            
            //double endDate = (double)[checkin objectForKey:@"endDate"];
            NSDictionary *customer = [checkin objectForKey:@"customer"];
            NSString *endType = [checkin objectForKey:@"endType"];
            BOOL isApproved = (BOOL)[checkin objectForKey:@"isApproved"];
        
            NSString *rowstr = [NSString stringWithFormat:@"%@ %3d分钟 %@", sdate, 30 + i, gymname];
            NSLog(@"%@", rowstr);
            [self.checkinArray addObject:rowstr];
            
            NSMutableArray *ss = [[NSMutableArray alloc] init];
            [ss addObject:@"aa"];
            
        }
        
        [self.historyTableView reloadData];
    
    } else {
        NSLog(@"Unexpected error");  
        [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
    }
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    NSError *error = [request error];
    NSLog(@"Error=%@", error.localizedDescription);   
    
    [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
}

#pragma mark - tableView delegate

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.checkinArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"didSelectRowAtIndexPath:%d",indexPath.row);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * showHistoryCellIdentifier = @"ShowHistoryCell"; 
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:showHistoryCellIdentifier];
    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:showHistoryCellIdentifier];        
    }
    cell.textLabel.text=[self.checkinArray objectAtIndex:indexPath.row];
    cell.accessoryType= UITableViewCellAccessoryNone;
    
    return cell;
}

@end
